﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace FetionLite
{
    class LED
    {
        class NLED_SETTINGS_INFO
        {
            public uint LedNum;
            public uint OffOnBlink;
            public int TotalCycleTime = 0;
            public int OnTime = 0;
            public int OffTime = 0;
            public int MetaCycleOn = 0;
            public int MetaCycleOff = 0;
        }

        class NLED_COUNT_INFO
        {
            public int cLeds = 0;
        }

        const int NLED_COUNT_INFO_ID = 0;
        const int NLED_SETTINGS_INFO_ID = 2;

        public enum Status
        {
            OFF = 0,
            ON,
            BLINK
        }

        [DllImport("coredll.dll")]
        private static extern bool NLedGetDeviceInfo(uint nID, NLED_COUNT_INFO pOutput);

        [DllImport("coredll.dll")]
        private static extern bool NLedSetDevice(uint nID, NLED_SETTINGS_INFO pOutput);

        //public LED()
        //{
        //    _ledCount = GetLedCount();
        //}

        public static void SetLedStatus(Status status)
        {

            NLED_SETTINGS_INFO nsi = new NLED_SETTINGS_INFO();

            nsi.OffOnBlink = (uint)status;

            int ledCount = GetLedCount();

            for (int i = 0; i < ledCount; i++)
            {
                nsi.LedNum = (uint)i;
                NLedSetDevice(NLED_SETTINGS_INFO_ID, nsi);
            }
        }

        public static void Vibrate(int millisecondsTimeout)
        {
            SetLedStatus(Status.ON);
            Thread.Sleep(millisecondsTimeout);
            SetLedStatus(Status.OFF);
        }

        private static int GetLedCount()
        {
            int count = 0;
            NLED_COUNT_INFO nci = new NLED_COUNT_INFO();
            if (NLedGetDeviceInfo(NLED_COUNT_INFO_ID, nci))
            {
                count = nci.cLeds;
            }

            return count;
        }
    }
}
